<?php
/**
 * 同步语音播放测试
 * 测试语音播放是否真的能听到声音
 */

echo "=== 同步语音播放测试 ===\n\n";

$voiceFile = __DIR__ . '/voice/checkin_success.mp3';

if (!file_exists($voiceFile)) {
    echo "❌ 语音文件不存在: {$voiceFile}\n";
    exit(1);
}

echo "✅ 语音文件存在: {$voiceFile}\n";
echo "文件大小: " . filesize($voiceFile) . " 字节\n\n";

// 测试1：使用 mciSendString (当前方法)
echo "【测试1】使用 mciSendString 播放 (当前方法)\n";
echo "准备播放...\n";

$realPath = realpath($voiceFile);
$tempScript = sys_get_temp_dir() . '/play_voice_test_' . uniqid() . '.ps1';

$psCode = str_replace('FILE_PATH', str_replace('\\', '\\\\', $realPath), <<<'POWERSHELL'
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class AudioPlayer {
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string command, System.Text.StringBuilder returnValue, int returnLength, IntPtr winHandle);
    
    public static void Play(string file) {
        mciSendString("close MediaFile", null, 0, IntPtr.Zero);
        mciSendString("open \"" + file + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
        mciSendString("play MediaFile", null, 0, IntPtr.Zero);
    }
}
"@
[AudioPlayer]::Play("FILE_PATH")
Write-Host "播放命令已执行"
Start-Sleep -Seconds 3
POWERSHELL
);

file_put_contents($tempScript, $psCode);

$command = 'powershell -WindowStyle Normal -ExecutionPolicy Bypass -File "' . $tempScript . '"';
echo "执行命令: {$command}\n";

// 使用 exec 同步执行
exec($command, $output, $returnCode);

echo "返回码: {$returnCode}\n";
echo "输出: " . implode("\n", $output) . "\n";

@unlink($tempScript);

echo "\n如果听到声音，说明播放成功！\n";
echo "如果没有听到声音，请检查：\n";
echo "1. 电脑音量是否打开\n";
echo "2. 扬声器/耳机是否正常工作\n";
echo "3. 是否有其他程序占用音频设备\n\n";

// 测试2：使用 start /wait 同步播放
echo "【测试2】使用 start /wait 同步播放\n";
echo "准备播放...\n";

$tempScript2 = sys_get_temp_dir() . '/play_voice_test2_' . uniqid() . '.ps1';
file_put_contents($tempScript2, $psCode);

$command2 = 'start /wait "" powershell -WindowStyle Normal -ExecutionPolicy Bypass -File "' . $tempScript2 . '"';
echo "执行命令: {$command2}\n";

exec($command2, $output2, $returnCode2);

echo "返回码: {$returnCode2}\n";
echo "输出: " . implode("\n", $output2) . "\n";

@unlink($tempScript2);

echo "\n=== 测试完成 ===\n";

